/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean.admin;

import com.max.oti.system.model.TypeComission;
import com.max.oti.system.service.interfac.ITypeComissionService;
import com.max.oti.system.util.FacesUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class TypeComissionBean implements Serializable {

    TypeComission typeComission = new TypeComission();
    @ManagedProperty("#{typeComissionService}")
    ITypeComissionService typeComissionService;
    List<TypeComission> listTypeComission;
    List<TypeComission> filterListTypeComission;
    
    @PostConstruct
    public void initCollection() {
        try {
            listTypeComission = typeComissionService.listHQL("from TypeComission t");
        } catch (Exception ex) {
            Logger.getLogger(TypeComissionBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void saveTypeComission() {

        if (typeComission.getId() == null) {
            try {
                typeComissionService.save(typeComission);
                listTypeComission.add(typeComission);
                typeComission = new TypeComission();
                FacesUtil.addMessage("exito", null, FacesMessage.SEVERITY_INFO);
            } catch (Exception ex) {
                FacesUtil.addMessage("Error", null, FacesMessage.SEVERITY_FATAL);
                Logger.getLogger(TypeComissionBean.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            try {
                typeComissionService.update(typeComission);
                listTypeComission = typeComissionService.listHQL("FROM TypeComission t");
                typeComission = new TypeComission();
                FacesUtil.addMessage("exito", null, FacesMessage.SEVERITY_INFO);
            } catch (Exception ex) {
                FacesUtil.addMessage("Error", null, FacesMessage.SEVERITY_FATAL);
                Logger.getLogger(TypeComissionBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }
//metodo para eliminar

    public void deleteTypeComission() {
        try {
            typeComissionService.delete(typeComission);
            listTypeComission = typeComissionService.listHQL("FROM TypeComission t");
            typeComission = new TypeComission();
        } catch (Exception ex) {
            Logger.getLogger(TypeComissionBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void onCellEdit(Object id, Object name) {
        if (name != null) {
            try {
                typeComissionService.HQLexecute("UPDATE TypeComission t SET t.name='" + name + "' WHERE t.id=" + id);
                FacesUtil.addMessage("Modificado con Exito", null, FacesMessage.SEVERITY_INFO);
            } catch (Exception ex) {
                Logger.getLogger(TypeUserBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    //<editor-fold defaultstate="collapsed" desc=" METODOS GETTER Y SETTER ">
    public TypeComission getTypeComission() {
        return typeComission;
    }

    public void setTypeComission(TypeComission typeComission) {
        this.typeComission = typeComission;
    }

    public ITypeComissionService getTypeComissionService() {
        return typeComissionService;
    }

    public void setTypeComissionService(ITypeComissionService typeComissionService) {
        this.typeComissionService = typeComissionService;
    }
    //para la lista

    public List<TypeComission> getListTypeComission() {
        if (listTypeComission == null) {
            try {
                listTypeComission = new ArrayList();//globalService.listHQL("FROM TypeComission");
            } catch (Exception ex) {
                Logger.getLogger(TypeComissionBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return listTypeComission;
    }

    public void setListTypeComission(List<TypeComission> listTypeComission) {
        this.listTypeComission = listTypeComission;
    }

    public List<TypeComission> getFilterListTypeComission() {
        return filterListTypeComission;
    }

    public void setFilterListTypeComission(List<TypeComission> filterListTypeComission) {
        this.filterListTypeComission = filterListTypeComission;
    }
    //</editor-fold> 
}